<?php

namespace app\common\model;

use think\Model;

/**
 * 配置模型
 */
class ChatDetail extends Model
{

    // 表名,不含前缀
    protected $name = 'chat_detail';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    // 追加属性
    protected $append = [
    ];

    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    /**
     * 关联获取用户信息
     */
    public function user(){
        return $this->belongsTo('User','user_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function chat(){
        return $this->belongsTo('Chat','chat_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function getList($where,$page){
        try{
            $data=$this
                ->with(['user','chat'])
                ->where($where)
                ->order('createtime desc')
                ->page($page)
                ->limit(10)
                ->select();

            foreach ($data as $row) {
                $row->visible(['id','chat_id','user_id','content','status','createtime']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['nickname','avatar']);
                $row->visible(['chat']);
                $row->getRelation('chat')->visible(['user_id','reply_user_id','something_id','createtime']);
            }
            if(!empty($data)){

                $data = collection($data)->toArray();

            }else{
                $data =[];
            }

        }catch (\Exception $e){
            return [];
        }

        return $data;

    }




}
